<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>登入</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/login.css" media="all">
</head>
<body>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">

    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2 style="color:#16baaa">青芽健身</h2>
        </div>
        <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                       for="LAY-user-login-username"></label>
                <input type="text" name="username" id="LAY-user-login-username" lay-verify="required"
                       placeholder="用户名"  value="admin" class="layui-input">
            </div>
            <div class="layui-form-item">
                <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                       for="LAY-user-login-password"></label>
                <input type="password" name="password" id="LAY-user-login-password" lay-verify="required"
                       placeholder="密码" value="123456" class="layui-input">
            </div>
            <div class="layui-form-item">
                <div class="layui-row">
                    <div class="layui-col-xs7">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                               for="LAY-user-login-vercode"></label>
                        <input type="text" name="captcha" id="LAY-user-login-vercode" lay-verify="required"
                               placeholder="图形验证码" class="layui-input">
                    </div>
                    <div class="layui-col-xs5">
                        <div style="margin-left: 10px;">
                            <img src="/sport/captcha" class="layadmin-user-login-codeimg" id="LAY-user-get-vercode">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="margin-bottom: 20px;">
                <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">
                <a href="forget.html" class="layadmin-user-jump-change layadmin-link"
                   style="margin-top: 7px;">忘记密码？</a>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit">登 入</button>
            </div>
        </div>
    </div>
</div>
<script src="../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', "useradmin"], function () {
        // jquery
        let $ = layui.$
            , form = layui.form;

        // 实现单击图片 改变验证码   /sport/captcha  -> /sport/captcha?time=1233456780
        $("#LAY-user-get-vercode").on('click', function () {
            $("#LAY-user-get-vercode").attr("src", "/sport/captcha?time=" + new Date().getTime())
        })

        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {
            // 1. 获取表单数据
            let formData = obj.field // {username:"",password:"",captcha:""}

            // 2.发送异步请求实现登录   /sport/user/login
            $.ajax({
                url: '/sport/user/login',  //  请求接口
                data: formData,  //  请求需要啊携带的数据
                type: 'GET',   //  设置请求的方式
                dataType: 'json', // 声明响应结果的数据格式
                success: function (res) {   //异步请求发送成功    会自动回调当前函数 同时将响应的结果传递在函数中
                    // 响应结果的处理
                    if (res.code == 0) {  //登录成功
                        // 信息提示
                        layer.msg(res.msg, {icon: 1}, function () {
                            // 将用户名 缓存在本地会话对象中   用于在主页面中显示用户信息
                            sessionStorage.setItem("userInfo", JSON.stringify(res.data))  // {nickname,username,password}
                            // 页面跳转  主页面
                            window.location.href = "/sport/admin/index.html"
                        })
                    } else {  //登录失败的处理方案
                        layer.msg(res.msg, {icon: 2})
                    }
                },
                error: function () {  // 异步请求发送失败
                    console.log("异步请求发送失败")
                }
            })
            // 3. 阻止表单的默认提交
            return false;
        });
    });
</script>
</body>
</html>